package cn.personal.library.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

import cn.personal.library.R;

/**
 * 横向进度条
 * <p>
 * <pre>
 * 添加命名空间：xmlns:zdxing="http://schemas.android.com/apk/res/包名
 *
 * &#60;HorizontalProgressBar
 * 	android:layout_width="match_parent"
 * 	android:layout_height="match_parent"
 * 	app:max="100"
 * 	app:progressDrawable="@color/white"
 * 	app:progress="100" /&#62;
 *
 * </pre>
 *
 * @author yangyong
 */
public class HorizontalProgressBar extends View {
    /** 进度条 */
    private Drawable procressDrawable;
    /** 最大值 */
    private int max;
    /** 当前进度 */
    private int progress;

    public HorizontalProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray typedArray = context.obtainStyledAttributes(attrs,
                R.styleable.HorizontalProgressBar);
        setMax(typedArray.getInt(R.styleable.HorizontalProgressBar_max, 100));
        setProgress(typedArray.getInt(R.styleable.HorizontalProgressBar_progress, 0));

        Drawable drawable = typedArray
                .getDrawable(R.styleable.HorizontalProgressBar_progressDrawable);
        if(drawable == null)
            drawable = new ColorDrawable(Color.parseColor("#6699FF"));
        setProcressDrawable(drawable);
        typedArray.recycle();
    }

    public HorizontalProgressBar(Context context) {
        this(context, null);
    }

    public HorizontalProgressBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float width = getWidth() - getPaddingLeft() - getPaddingRight();
        float d = width / max;

        float progressWidth = d * progress;
        if(procressDrawable != null) {
            int left = getPaddingLeft();
            int top = getPaddingTop();
            int right = (int) progressWidth + left;
            int bottom = getHeight() - getPaddingBottom();
            canvas.save();
            canvas.clipRect(left, top, right, bottom);
            procressDrawable.setBounds(left, top, right, bottom);
            procressDrawable.draw(canvas);
            canvas.restore();
        }
    }

    public Drawable getProcressDrawable() {
        return procressDrawable;
    }

    public void setProcressDrawable(Drawable procressDrawable) {
        this.procressDrawable = procressDrawable;
    }

    public int getMax() {
        return max;
    }

    public void setMax(int max) {
        this.max = max;
        invalidate();
    }

    public int getProgress() {
        return progress;
    }

    public void setProgress(int progress) {
        this.progress = progress;
        invalidate();
    }

}
